#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>

static int fd, event;
static uint8_t button, value;

static void signal_handler(int sig){
    read(fd, &event, sizeof(event));
    button = (event >> 8) & 0xff; 
    value = (event) & 0xff;
    printf("[%s]:button event : butto %u value %u\n",
            __func__, (unsigned int)button, (unsigned int)(value));
}

int main(int argc, char *argv[]){
    if(argc != 2){
        printf("[%s]:Usage:%s <file>\n", argv[0], argv[1]);
        return -1;
    }

    signal(SIGIO, signal_handler);


    fd = open(argv[1], O_RDWR);
    if(fd < 0){
        perror("open");
        return -1;
    }
    
    fcntl(fd, F_SETOWN, getpid());

    int flags = fcntl(fd, F_GETFL);

    fcntl(fd, F_SETFL, flags | FASYNC);

    
    while(1){
        sleep(10);
        printf("[%s] funciton calld\n", argv[0]);
    }

    close(fd);
    return 0;
}